<template>
  <div id="history">
    <van-cell title="搜索历史">
      <template>
        <div class="right" v-if="isDel">
          <!-- 还可以子向父传 $emit父组件的history等于一个 [] -->
          <span @click="history.splice(0)">全部删除</span>
          <span @click="isDel = false">完成</span>
        </div>
        <van-icon @click="isDel = true" v-else name="delete-o" />
      </template>
    </van-cell>
    <van-cell
      @click="onClickHistoryItem(i)"
      :title="item"
      v-for="(item, i) in history"
      :key="item"
    >
      <template>
        <van-icon v-if="isDel" name="close" />
      </template>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'history',
  props: {
    history: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDel: false
    }
  },

  // 定义方法
  methods: {
    onClickHistoryItem (index) {
      if (this.isDel) {
        this.history.splice(index, 1)
      } else {
        this.$emit('search', this.history[index])
      }
    }
  }
}
</script>

<style scoped lang="less">
#history {
  .van-cell {
    .right {
      span:nth-child(1) {
        margin-right: 8px;
      }
    }
  }
}
</style>
